home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / vgl20.zip / VGLLINE.C < prev    next >
Text File  |  1993-05-14  |  1KB  |  64 lines

  1. /*****************************************************************************
  2.  VGLLINE.C
  3.  
  4.  vglLine( int x1, int y1, int x2, int y2, int c );
  5.  
  6.  Draws a line from x1,y1 to x2,y2 in the color c.  Fairly quick, even though
  7.  it's in C.  Original algorithm from Graphics Gems.  This is an *excellent*
  8.  general purpose Bresanham line tracing routine!
  9.  
  10.  Mark
  11.  morley@camosun.bc.ca
  12. *****************************************************************************/
  13.  
  14.  #include "vgl.h"
  15.  
  16. #define ABS(a)   ((a < 0) ? -a : a)
  17. #define SGN(a)   ((a < 0) ? -1 : 1)
  18.  
  19. vglLine( int x1, int y1, int x2, int y2, int c )
  20. {
  21.    int d, x, y, ax, ay, sx, sy, dx, dy;
  22.  
  23.    dx = x2-x1;
  24.    ax = ABS(dx) << 1;
  25.    sx = SGN(dx);
  26.    dy = y2-y1;
  27.    ay = ABS(dy) << 1;
  28.    sy = SGN(dy);
  29.  
  30.    x = x1;
  31.    y = y1;
  32.    if( ax > ay )
  33.    {
  34.       d = ay - (ax >> 1);
  35.       while( x != x2 )
  36.       {
  37.          vglPutPel( x, y, c );
  38.      if( d >= 0 )
  39.          {
  40.             y += sy;
  41.         d -= ax;
  42.          }
  43.      x += sx;
  44.      d += ay;
  45.       }
  46.    }
  47.    else
  48.    {
  49.       d = ax - (ay >> 1);
  50.       while( y != y2 )
  51.       {
  52.          vglPutPel( x, y, c );
  53.      if( d >= 0 )
  54.          {
  55.             x += sx;
  56.         d -= ay;
  57.          }
  58.      y += sy;
  59.      d += ax;
  60.       }
  61.    }
  62.    return 0;
  63. }
  64.